<template>
  <Dialog
    :show="dialogConfig.show"
    :title="dialogConfig.title"
    :buttons="dialogConfig.buttons"
    :showCancel="true"
    @close="dialogConfig.show = false"
  >
    <el-form
      :model="formData"
      :rules="rules"
      ref="formDataRef"
      label-width="80px"
      @submit.prevent
    >
      <el-form-item label="学工号" prop="userName">
        <el-input :maxLength="20" v-model="formData.userName" />
      </el-form-item>

      <el-form-item label="姓名" prop="name">
        <el-input :maxLength="30" v-model="formData.name" />
      </el-form-item>

      <el-form-item label="邮箱" prop="email">
        <el-input :maxLength="50" v-model="formData.email" />
      </el-form-item>

      <template v-if="formData.userId==null">
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            size="large"
            placeholder="请输入密码"
            v-model.trim="formData.password"
          >
            <template #prefix>
              <span class="iconfont icon-password"></span>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="rePassword">
          <el-input
            type="password"
            size="large"
            placeholder="请再次输入密码"
            v-model.trim="formData.rePassword"
          >
            <template #prefix>
              <span class="iconfont icon-password"></span>
            </template>
          </el-input>
        </el-form-item>
      </template>

        <el-form-item label="角色" prop="roleType">
         <el-radio-group v-model="formData.roleType">
            <el-radio :value="0">志愿者</el-radio>
            <el-radio :value="1">平台管理员</el-radio>
            <el-radio :value="2">后台管理员</el-radio>
         </el-radio-group>
      </el-form-item>
    </el-form>
  </Dialog>
</template>

<script setup>
import { ref, reactive, getCurrentInstance, nextTick } from "vue";
const { proxy } = getCurrentInstance();

const api = {
  saveAccount: "/userInfo/saveUserInfo",
};


const dialogConfig = ref({
  show: false,
  title: "新增用户",
  buttons: [
    {
      type: "primary",
      text: "确定",
      click: (e) => {
        submitForm();
      },
    },
  ],
});

const formData = ref({
});
const formDataRef = ref();

const validateRePass = (rule, value, callback) => {
  if (value !== formData.value.password) {
    callback(new Error(rule.message));
  } else {
    callback();
  }
};

const rules = {
  userName: [{ required: true, message: "请输入学工号" }],
  name: [{ required: true, message: "请输入用户名" }],
  password: [
    { required: true, message: "请输入密码" },
    {
      validator: proxy.Verify.password,
      message: "密码最少8位，只能数字字母和特殊字符",
    },
  ],
  rePassword: [
    { required: true, message: "请再次输入密码" },
    {
      validator: validateRePass,
      message: "两次输入的密码不一致",
    },
  ],
  phone: [
    { required: true, message: "请输入手机号" },
    {
      validator: proxy.Verify.phone,
      trigger: "blur",
      message: "请输入正确的手机号",
    },
  ],
  email: [
    { required: true, message: "请输入邮箱" },
    {
      validator: proxy.Verify.email,
      trigger: "blur",
      message: "请输入正确的邮箱",
    },
  ],
  roleType: [{ required: true, message: "请选择角色" }],
};

const showEdit = (data) => {
  dialogConfig.value.show = true;
  nextTick(() => {
    formDataRef.value.resetFields();
    if (data.userId == null) {

      dialogConfig.value.title = "新增账号";
      formData.value.userId=null;
    } else {
      dialogConfig.value.title = "修改账号";
      let editData = Object.assign({}, data);
      formData.value = Object.assign({}, editData);
    }
  });
};

defineExpose({
  showEdit,
});

const emit = defineEmits(["reload"]);
const submitForm = async () => {
  formDataRef.value.validate(async (valid) => {
    if (!valid) {
      return;
    }
    let params = {};
    Object.assign(params, formData.value);

    let result = await proxy.Request({
      url: api.saveAccount,
      params,
    });
    if (!result) {
      return;
    }
    dialogConfig.value.show = false;
    proxy.Message.success("保存成功");
    emit("reload");
  });
};
</script>

<style lang="scss" scoped>
.check-span-item {
  float: left;
  margin-right: 10px;
  line-height: 20px;
}
</style>
